// prototype patch by xlive
Object.extend(Object, {
  implement: function(destination, source) {
  	for(var property in source) {
    	if(Object.isUndefined(destination[property])) {
			if(property.endsWith('_properties')){
				destination[property]=(destination[property]||{});
				Object.extend(destination[property], Object.toJSON(source[property]).evalJSON());
			}
			else destination[property]=source[property];
		}
  	}
  	return destination;
  }
}); 
Object.extend(Object, {
	toJSON:function(object) {
    var type = typeof object;
    switch (type) {
      case 'undefined':
      case 'unknown': return;
      case 'boolean': return object.toString();
      case 'function':return object.toString();
    }
    if (object === null) return 'null';
    if (object.toJSON) return object.toJSON();
    if (Object.isElement(object)) return;
    var results = [];
    for (var property in object) {
      var value = Object.toJSON(object[property]);
      if (!Object.isUndefined(value))
        results.push(property.toJSON() + ': ' + value);
    }
    return '{' + results.join(', ') + '}';
  }
}); 


if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, {
  escapeXMLString: function() {
    return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\"/g,'&guot;');
  },
  unescapeXMLString: function() {
    return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&guot;/, '\"');
  }
});
Prototype.Browser.IE7=(Prototype.Browser.IE && navigator.userAgent.indexOf('MSIE 7.') > -1);
Prototype.Browser.MobileSafari=/Apple.*Mobile/.test(navigator.userAgent);

Date.prototype.parseDate=function(datestring,pattern) {
	try{
		if(!pattern){
			var r=/(\d{4}|\d{3}|\d{2})\D*(\d{2}|\d{1}|\d{0})\D*(\d{2}|\d{1}|\d{0})\D*(\d{2}|\d*)\D*(\d{2}|\d*)\D*(\d{2}|\d*)\D*(\d{3}|\d*)/;
			datestring.match(r);
			var year=(!RegExp.$1)?1970:(RegExp.$1.length==3)?1911+RegExp.$1:(RegExp.$1.length==2)?1900+RegExp.$1:RegExp.$1;
			this.setFullYear(year,(RegExp.$2||'01')-1,(RegExp.$3||'01'));
			this.setHours((RegExp.$4||'00'),(RegExp.$5||'00'),(RegExp.$6||'00'),(RegExp.$7||'00'));
		}else{
			var yy,MM,dd,HH,mm,ss,i;
			yy=((i=pattern.indexOf('yyyy'))>=0)?parseInt(datestring.substr(i,4)):null;
			if(!yy)yy=((i=pattern.indexOf('cyy'))>=0)?parseInt(datestring.substr(i,3))+1911:null;
			MM=((i=pattern.indexOf('MM'))>=0)?parseInt(datestring.substr(i,2)):null;
			dd=((i=pattern.indexOf('dd'))>=0)?parseInt(datestring.substr(i,2)):null;
			HH=((i=pattern.indexOf('HH'))>=0)?parseInt(datestring.substr(i,2)):null;
			mm=((i=pattern.indexOf('mm'))>=0)?parseInt(datestring.substr(i,2)):null;
			ss=((i=pattern.indexOf('ss'))>=0)?parseInt(datestring.substr(i,2)):null;
			if(yy!=null) this.setFullYear(yy);
			if(MM!=null&&MM>0) this.setMonth(MM-1);
			if(dd!=null&&dd>0) this.setDate(dd);
			if(HH!=null) this.setHours(HH);
			if(mm!=null) this.setMinutes(mm);
			if(ss!=null) this.setSeconds(ss);
			this.pattern=pattern;
			this.validate=
			((yy!=null)?(this.getFullYear()==yy):true)&&
			((MM!=null&&MM>0)?(this.getMonth()+1==MM):true)&&
			((dd!=null&&dd>0)?(this.getDate()==dd):true)&&
			((HH!=null)?(this.getHours()==HH):true)&&
			((mm!=null)?(this.getMinutes()==mm):true)&&
			((ss!=null)?(this.getSeconds()==ss):true);
		}
	}catch(e){
		alert('parseDate error :'+e);	
	}
	return this;
};
Date.prototype.formatDate=function(pattern){
	var str=(pattern||'yyyyMMddHHmmssS');
	str=str.replace(/yyyy/g,this.getFullYear());
	var tmp='000'+(this.getFullYear()-1911);
	str=str.replace(/cyy/g,tmp.substring(tmp.length-3));
	tmp='00'+(this.getMonth()+1);
	str=str.replace(/MM/g,tmp.substring(tmp.length-2));
	tmp='00'+this.getDate();
	str=str.replace(/dd/g,tmp.substring(tmp.length-2));
	tmp='00'+this.getHours();
	str=str.replace(/HH/g,tmp.substring(tmp.length-2));
	tmp='00'+this.getMinutes();
	str=str.replace(/mm/g,tmp.substring(tmp.length-2));
	tmp='00'+this.getSeconds();
	str=str.replace(/ss/g,tmp.substring(tmp.length-2));
	tmp='000'+this.getMilliseconds();
	str=str.replace(/S/g,tmp.substring(tmp.length-3));
	return str;
};
Date.prototype.isLeap=function(year) {
	return ((year%4==0)&&(y%100!=0))||(y%400==0);
};
Date.prototype.setLastDate=function() {
	var d=new Date(this.getFullYear(),this.getMonth()+1,1);
	this.setDate(new Date(d.getTime()-8600000).getDate());
	return this;
};
Date.prototype.nextDate=function(dayoffset){
	this.setTime(this.getTime()+(dayoffset*86400000));
	return this;
};
Date.prototype.cloneDate=function() {
	return new Date(this.getTime());
};






